package com.bigegg.employee.service.impl;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

import com.alibaba.excel.EasyExcel;
import com.bigegg.employee.domain.College;
import com.bigegg.employee.domain.Major;
import com.bigegg.employee.mapper.MajorMapper;
import com.ruoyi.common.utils.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.bigegg.employee.mapper.ClassesMapper;
import com.bigegg.employee.domain.Classes;
import com.bigegg.employee.service.IClassesService;
import org.springframework.web.multipart.MultipartFile;

/**
 * 专业Service业务层处理
 * 
 * @author 刘博
 * @date 2022-08-11
 */
@Service
public class ClassesServiceImpl implements IClassesService 
{

    public final Logger log = LoggerFactory.getLogger(ClassesServiceImpl.class);

    @Autowired
    private ClassesMapper classesMapper;

    @Autowired
    private MajorMapper majorMapper;

    /**
     * 查询专业
     * 
     * @param id 专业主键
     * @return 专业
     */
    @Override
    public Classes selectClassesById(Long id)
    {
        return classesMapper.selectClassesById(id);
    }

    /**
     * 查询专业列表
     * 
     * @param classes 专业
     * @return 专业
     */
    @Override
    public List<Classes> selectClassesList(Classes classes)
    {
        return classesMapper.selectClassesList(classes);
    }

    /**
     * 新增专业
     * 
     * @param classes 专业
     * @return 结果
     */
    @Override
    public int insertClasses(Classes classes)
    {
        classes.setCreateTime(DateUtils.getNowDate());
        return classesMapper.insertClasses(classes);
    }

    /**
     * 修改专业
     * 
     * @param classes 专业
     * @return 结果
     */
    @Override
    public int updateClasses(Classes classes)
    {
        return classesMapper.updateClasses(classes);
    }

    /**
     * 批量删除专业
     * 
     * @param ids 需要删除的专业主键
     * @return 结果
     */
    @Override
    public int deleteClassesByIds(Long[] ids)
    {
        return classesMapper.deleteClassesByIds(ids);
    }

    /**
     * 删除专业信息
     * 
     * @param id 专业主键
     * @return 结果
     */
    @Override
    public int deleteClassesById(Long id)
    {
        return classesMapper.deleteClassesById(id);
    }

    /**
     * 上传excel
     */
    @Override
    public void importData(MultipartFile multipartFileile){

        InputStream is = null;
        try {
            is = multipartFileile.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }

        List<Classes> list = EasyExcel.read(new BufferedInputStream(is)).head(Classes.class).sheet().headRowNumber(1).doReadSync();

        if(list!=null && list.size()>0){

            Long cnt = 0L;

            for(Classes classes : list){

                //
                Classes c = new Classes();

                c.setName(classes.getName());

                List<Classes> classesList = this.classesMapper.selectClassesList(c);

                if(classesList==null || classesList.size()==0){

                    classes.setSort(cnt);
                    classes.setIsDel(0L);
                    classes.setCreateTime(new Date());

                    // 专业id
                    Major major = new Major();
                    major.setName(classes.getMajorName());
                    List<Major> majorList = this.majorMapper.selectMajorList(major);
                    if(majorList!=null && majorList.size()>0){
                        Major col = majorList.get(0);
                        classes.setMajorId(col.getId());
                    }

                    this.classesMapper.insertClasses(classes);

                }else {

                    this.log.info("此条已存在，无需新增，进行关联");
                }

                cnt++;
                this.log.info(String.format("已完成%d/%d",cnt,list.size()));
            }

        }
    }
}
